package com.zhangli.saveobject;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;

import com.zhangli.fingerprint.R;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    @InjectView(R.id.save_button)
    Button save_button;
    @InjectView(R.id.save_text)
    TextView save_text;
    @InjectView(R.id.get_button)
    Button get_button;
    @InjectView(R.id.get_text)
    TextView get_text;

    private User user;
    private static String USER="user";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_layout);

        ButterKnife.inject(this);

        user=new User();
        user.setId(999);
        user.setName("zhangli");
    }

    @OnClick(R.id.save_button)
    void saveClick(){
        try {
            saveObject(serialize(user),getApplicationContext(),USER);
            save_text.setText("存入对象的id:"+user.getId()+",名字："+user.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @OnClick(R.id.get_button)
    void getClick(){
        try {
            User newUser= (User) deSerialization(getObject(getApplicationContext(),USER));
            get_text.setText("得到对象的id:"+newUser.getId()+",名字："+newUser.getName());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 序列化对象
     *
     * @param person
     * @return
     * @throws IOException
     */
    public static String serialize(Object person) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(person);
        String serStr = byteArrayOutputStream.toString("ISO-8859-1");
        serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
        objectOutputStream.close();
        byteArrayOutputStream.close();
        return serStr;
    }

    /**
     * 反序列化对象
     *
     * @param str
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Object deSerialization(String str) throws IOException, ClassNotFoundException {
        String redStr = java.net.URLDecoder.decode(str, "UTF-8");
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        Object person = objectInputStream.readObject();
        objectInputStream.close();
        byteArrayInputStream.close();
        return person;
    }

    /**
     * 存对象
     *
     * @param strObject 对象
     * @param context
     * @param name      存储名
     */
    public static void saveObject(String strObject, Context context, String name) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(name, strObject);
        edit.commit();
    }

    /**
     * 取对象
     *
     * @param context
     * @param name    存储名
     * @return
     */
    public static String getObject(Context context, String name) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return sp.getString(name, "");
    }
}
